package com.cremamobile.filemanager.file;

import java.io.File;

import java.util.List;

import com.cremamobile.filemanager.CremaDialog;
import com.cremamobile.filemanager.R;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;

public class FileCopyer extends FileWorker
{
//	public final static int COPY_MODE_NOT_SET = 0;
//	public final static int COPY_MODE_REPLACE = 1;
//	public final static int COPY_MODE_RENAME = 2;
	
	private FileListEntry mDestDir;
	private long totalSize;
	private long currentSize = 0;

	public FileCopyer(Context context, List<FileListEntry> files, FileListEntry destDir, ResultCallbacks cb) {
		super(context, files, cb);
		this.mDestDir= destDir;
	}
	
	@Override
	public Boolean working() {
		// TODO Auto-generated method stub
		boolean result = true;
		
		try {
			
		int count = mFiles.size();
		
		while( ! mFlag.isAborted() && mStep < count) {
			FileListEntry item = mFiles.get(mStep);
			
			String target = mDestDir.getAbsolutePath() + "/" + item.getFullName();
	
			// 같은 파일이 존재한다면...
			if (FileUtils.existFile(target)) {
				String tempTarget = FileUtils.getRenameWithExisting((DirFileEntry)mDestDir, item);
				
				if (mWorkMode == MODE_REPLACE) {
					//덮어쓰기
					result = FileUtils.copyFromFileChannel(item.getPath(), new File(tempTarget));
					if (result == true) {
						FileUtils.delete(target);
						FileUtils.rename(tempTarget, target);
					}
				} else if (mWorkMode == MODE_RENAME){
					result = FileUtils.copyFromFileChannel(item.getPath(), new File(tempTarget));
				}
				
				mModeItemCnt++;
//				mProgressDialog.setContentText(mCaller.getResources().getString(R.string.copy_dialog_message,
//						i+1, count));
//				
//				mProgressDialog.setContentSizeText(mCaller.getResources().getString(R.string.file_work_rename,
//						"0MB"));
//				mProgressDialog.setProgress(i);
			} else {
				result = FileUtils.copyFromFileChannel(item.getPath(), new File(target));
			}

			
			if ( ! result ) {
				//TODO. 
				break;
			}
			
			mStep++;
			publishProgress(mStep);
		}
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	@Override
	public String getPreExecuteDialogMessage(int step) {
		// TODO Auto-generated method stub
		return mCaller.getString(R.string.copy_dialog_message, step, mFiles.size());
	}
	
	@Override
	public String getPostExecuteToastMessage(boolean success) {
		// TODO Auto-generated method stub
		if (success) {
			
			if (mModeItemCnt >= 0) {
				if (mWorkMode == MODE_RENAME) {
					return mCaller.getString(R.string.toast_copy_rename_success, mFiles.size(), mModeItemCnt);
				} else if (mWorkMode == MODE_REPLACE) {
					return mCaller.getString(R.string.toast_copy_all_file_replace_success, mFiles.size(), mModeItemCnt);
				}
			}
			return mCaller.getString(R.string.toast_copy_success, mFiles.size());
		}
		else
			return mCaller.getString(R.string.toast_copy_cancel, mFiles.size(), mStep);
	}
	
	@Override
	public String getProgressContentMessageByStep(int step) {
		return mCaller.getResources().getString(R.string.copy_dialog_message, step, mFileCount);
	}
	
	@Override
	public String getProgressSizeMessageByStep(int step) {
		return mCaller.getResources().getString(R.string.file_work_rename, FileUtils.formatSize(sumSize(mFiles, step)));
	}

	@Override
	public int type() {
		// TODO Auto-generated method stub
		return FILE_COPYER;
	}

	@Override
	public FileListEntry getDestination() {
		// TODO Auto-generated method stub
		return mDestDir;
	}	
}